var user={};
jQuery(document).ready(function() {
	
	 $('#reserveApply').click(function(){ 
		 //$('#reserveApply').modal('show');
		 $("#cspiframe").attr("src","../reserveAdd/index.html?r="+Math.random());
	 });
	
	/*获取当前用户*/
	getUser();
			
	//bindOrg("orgSelect","2","0","");
	
	bindOrg2("orgSelect");
						
	//列表查询事件
	$('#search').click(function(){
		$('#bookingTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
				
});	

function getUser(){	
	$.ajax({
		  type: 'POST',
		  url: '../../user/getUser.do',
		  contentType : 'application/json',
		  data:JSON.stringify({}),
		  dataType: 'json',
		  success: function(data){
			  if(data.result=="1"){
				  user=data; 
				  initTable(); 
			  }else{
				  user.proposerid="无";
				  initTable();
			  }
			  
		  }
	})
}

function initTable(){	
	$('#bookingTable').bootstrapTable({
		method: 'post',
	    url: basePath() + 'reserve/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	ORGCODE: $('#orgSelect').attr("data"),
                	RECORDDATE: $('#recordDate').val(),
                	USERID:user.proposerid
                };
        },
	    columns: [{  
            field : 'BESPEAKNO',  
            title : '预约序号',  
            align : 'center'
	    }, {
	        field: 'ITEMNAME',
	        title: '预约项目',  
	        align : 'center'
	    }, {
	        field: 'RECORDID',
	        visible : false	        
	    }, {	    	
	        field: 'ORGNAME',
	        title: '预约机构',
	        align : 'center'

	    }, {
	        field: 'RECORDDATE',
	        title: '预约日期',
		    align : 'center'	        	
	    }, {
	        field: 'RECORDPERIOD',
	        title: '预约时间',
		    align : 'center'
	    },{
            field: 'operate',
            title: '取消预约',
            align: 'center',
            width: '200px',
            events: operateEvents,
            formatter: function(value, row, index){
            	var a=row.RECORDPERIOD;
            	var b=a.indexOf("-");
            	a=a.substring(b+1,a.lenght);
            	var time=row.RECORDDATE+a;
            	var date =new Date(time);
            	var c = new Date();
            	var d=c.getFullYear()+"-"+(c.getMonth()+1)+"-"+(c.getDate()+1)+" 00:00:00";
            	var now =new Date(d);
            	
            	var boo=false;
            	if(date<now){
            		boo=true;
            	}
            	if(boo==true){
            		return [
                		'<button type="button" disabled="disabled" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>取消</button>'
                	].join(''); 	
            	}else{
            		return [
                		'<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>取消</button>'
                	].join('');
            	}           	           	           	
            }
	    }]
	});
}

window.operateEvents={				
		'click .Delete' : function(e, value, row, index) {			
			 $.confirm({
		            title: '操作提示！',
		    	    content: '是否<strong>取消</strong>预约数据?',
		            buttons: {
		                ok: {
		                    text: "确定",
		                    btnClass: 'btn-primary',
		                    keys: ['enter'],
		                    action: function(){
		                    	$.ajax({
		            				url : projectPath + 'reserve/delete.do',
		            				data : {
		            					"RECORDID" : row.RECORDID
		            				},
		            				dataType : "json",
		            				success : function(data) {
		            					if (data.result == 1) {
		            						$.alert('取消预约成功!');
		            						$('#bookingTable').bootstrapTable(
		            						('refresh')); // 很重要的一步，刷新url！
		            					}
		            				}
		            			});		        				
		                    }
		                },
		                cancel: {
		                    text: "取消",
		                    keys: ['esc']
		                }
		            }
		        })
			
		}
		
}

//ajax获取机构数据
function bindOrg(selectID,orgLevel,orgType,selectdata){
	var infoUrl = projectPath + "share/getOrg1.do";
	$.ajax({ 
		url: infoUrl,
		data: {"orgLevel": orgLevel,
				"orgType":orgType
			},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas.datas;	
				console.log(data);
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].orgCode;
					var text = dataStr[i].orgName;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}
    	}
	});
	
}


function bindOrg1(obj){
	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg2.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
		    var cityPicker = new IIInsomniaCityPicker({
		        data: data,
		        target: '#' + obj,
		        callback: function(id){
		        	var id = $('#' + obj).attr("data");
		        	var name = $('#' + obj).val();
		        }
		    });

		    cityPicker.init();
		}
	});
}

function bindOrg2(obj){
	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			//常用海事机构
			var historyjson = data.hisResult;
			//海事机构
			var orgAlljson = data.result;
			
			var topCode = '000000';
			var orgList = JSON2ARRAY3(orgAlljson, topCode);
			var orgHistoryList = JSON2ARRAY2(historyjson);
			InitList(obj, topCode, orgList, orgHistoryList);
		}
	});
}

